﻿using System;
using System.Collections.Generic;
using System.Text;
using Tunynet.QingdaoCustoms.Data.Utils;
using Tunynet.QingdaoCustoms.Common;

namespace ChinaCustoms.Qingdao.Workflow.Supports
{
    /// <summary>
    /// 任务业务逻辑类
    /// </summary>
    public class Tasks
    {
        /// <summary>
        /// 创建任务
        /// </summary>
        public static void CreateTask(TaskInfo taskInfo)
        {
            BusinessDataProvider.Instance().CreateUpdateTask(taskInfo, DataProviderAction.Create);
        }

        /// <summary>
        /// 更新任务
        /// </summary>
        public static void UpdateTask(TaskInfo taskInfo)
        {
            BusinessDataProvider.Instance().CreateUpdateTask(taskInfo, DataProviderAction.Update);
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        public static void DeleteTask(Guid taskGuid)
        {
            BusinessDataProvider.Instance().DeleteTask(taskGuid);
        }

        /// <summary>
        /// 完成任务
        /// </summary>
        /// <param name="taskGuid">TaskID</param>
        /// <param name="lastWorkItemGuid">办结任务的工作项ID</param>
        public static void CompleteTask(Guid taskGuid, Guid lastWorkItemGuid)
        {
            TaskInfo task = GetTask(taskGuid);
            if (task != null)
            {
                task.LastWorkItemGuid = lastWorkItemGuid;
                UpdateTask(task);

                BusinessDataProvider.Instance().CompleteTask(taskGuid, false);
            }
        }


        /// <summary>
        /// 终止任务
        /// </summary>
        /// <param name="taskGuid">TaskID</param>
        public static void TerminateTask(Guid taskGuid)
        {
            BusinessDataProvider.Instance().CompleteTask(taskGuid, true);
        }


        /// <summary>
        /// 尝试完成Task
        /// </summary>
        /// <remarks>
        /// 结束依据：Task的所有SubTask及WorkItem都已经得到处理
        /// </remarks>
        /// <param name="taskGuid">TaskGuid</param>
        public static bool TryCompleteTask(Guid taskGuid)
        {
            return BusinessDataProvider.Instance().TryCompleteTask(taskGuid);
        }


        /// <summary>
        /// 重新启动已经结束的任务
        /// </summary>
        /// <remarks>
        /// 终止的任务不能重新启动
        /// </remarks>
        /// <param name="taskGuid">taskGuid</param>
        public static void RestartTask(Guid taskGuid)
        {
            TaskInfo task = GetTask(taskGuid);
            if (task != null && task.Status == TaskStatus.Completed && task.LastWorkItemGuid != Guid.Empty)
            {
                BusinessDataProvider.Instance().RestartTask(taskGuid, task.LastWorkItemGuid);
            }
        }

        /// <summary>
        /// 通过WorkflowID获取任务
        /// </summary>
        public static TaskInfo GetTaskByWorkflowGuid(Guid workflowGuid)
        {
            return BusinessDataProvider.Instance().GetTask(null, workflowGuid);
        }

        /// <summary>
        /// 获取任务
        /// </summary>
        public static TaskInfo GetTask(Guid taskGuid)
        {
            return BusinessDataProvider.Instance().GetTask(taskGuid, null);
        }

        /// <summary>
        /// 通过查询条件获取任务，并用于分页显示
        /// </summary>
        public static PagingDataSet<TaskInfo> GetTasks(TaskQuery taskQuery)
        {
            return BusinessDataProvider.Instance().GetTasks(taskQuery);
        }

        ///// <summary>
        ///// 获取用户相关的Task
        ///// </summary>
        ///// <param name="userName">用户名</param>
        //public static List<TaskInfo> GetUserParticipantTasks(string userName)
        //{
        //    return BusinessDataProvider.Instance().GetUserParticipantTasks(userName, null);
        //}

        /// <summary>
        /// 获取用户相关的Task
        /// </summary>
        /// <param name="userName">用户名</param>
        public static List<TaskInfo> GetUserParticipantTasks(string userName, string applicationCodeName)
        {
            return BusinessDataProvider.Instance().GetUserParticipantTasks(userName, applicationCodeName, null);
        }



        /// <summary>
        /// 判断用户是否Task的参与者
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="taskID">TaskID</param>
        /// <param name="subFlowDictionary">如果用户是子流程的参与者则返回所参与的子流程列表</param>
        /// <returns><see cref="ChinaCustoms.Qingdao.Workflow.Supports.TaskActorType"/></returns>
        public static TaskActorType GetTaskActorType(string userName, Guid taskGuid, out Dictionary<Guid, SubTaskInfo> subTaskDictionary)
        {
            subTaskDictionary = new Dictionary<Guid, SubTaskInfo>();

            //获取到某人在某任务下参与的所有工作项
            List<WorkItemInfo> originalWorkItems = BusinessDataProvider.Instance().GetWorkItems(null, taskGuid, null, userName);
            foreach (WorkItemInfo workItem in originalWorkItems)
            {
                if (workItem.SubTaskGuid == Guid.Empty)
                {
                    return TaskActorType.MainFlowActor;
                }
                else
                {
                    if (!subTaskDictionary.ContainsKey(workItem.SubTaskGuid))
                    {
                        SubTaskInfo subTask = SubTasks.GetSubTask(workItem.SubTaskGuid);
                        if (subTask != null)
                        {
                            subTaskDictionary[workItem.SubTaskGuid] = subTask;
                            GetRecursiveParentSubTask(subTask.ParentSubTaskGuid,ref subTaskDictionary);
                            GetRecursiveChilderenSubTask(subTask.SubTaskGuid, ref subTaskDictionary);
                        }
                    }
                }
            }

            if (subTaskDictionary.Count > 0)
            {
                return TaskActorType.SubFlowActor;
            }
            else
            {
                return TaskActorType.NotActor;
            }
        }
        
        /// <summary>
        /// 通过递归方式获取某个子任务的父亲任务(既父亲, 父亲的父亲,父亲的父亲的父亲等)
        /// </summary>
        private static void GetRecursiveParentSubTask(Guid parentSubTaskGuid ,ref Dictionary<Guid, SubTaskInfo> subTaskDictionary)
        {
            if (parentSubTaskGuid != Guid.Empty&& subTaskDictionary.ContainsKey(parentSubTaskGuid)==false)
            {
                SubTaskInfo parentSubTaskInfo = SubTasks.GetSubTask(parentSubTaskGuid);
                if (parentSubTaskInfo != null)
                {
                    subTaskDictionary[parentSubTaskGuid] = parentSubTaskInfo;

                    if (parentSubTaskInfo.ParentSubTaskGuid != Guid.Empty)
                    {
                        GetRecursiveParentSubTask(parentSubTaskInfo.ParentSubTaskGuid,ref subTaskDictionary);
                    }
                }
            }
        }

        /// <summary>
        /// 通过递归的方式获取某个任务的所以子任务
        /// </summary>
        private static void GetRecursiveChilderenSubTask(Guid subTaskGuid, ref Dictionary<Guid, SubTaskInfo> subTaskDictionary)
        {
            //if (subTaskDictionary.ContainsKey(subTaskGuid) == false)
            {
                List<SubTaskInfo> subTaskList=  SubTasks.GetSubTasksByParentSubTaskGuid(subTaskGuid,null);
                if (subTaskList != null)
                {
                    for (int i = 0; i < subTaskList.Count; i++)
                    {
                        if (subTaskDictionary.ContainsKey(subTaskList[i].SubTaskGuid) == false)
                        {
                            subTaskDictionary[subTaskList[i].SubTaskGuid] = subTaskList[i];
                        }

                        GetRecursiveChilderenSubTask(subTaskList[i].SubTaskGuid,ref subTaskDictionary);
                    }
                }
            }
        }
    }
}
